/*
 * @(#)FinderType.java
 *
 *
 *
 * ver 1.0 Aug 16, 2007 ggranum
 */
package org.testfire.model;

import java.util.HashMap;
import java.util.Map;
import org.testfire.util.DomUtil;
import org.testfire.translator.TranslatorFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

public class FinderType implements TestFireObject
{
    private TestFireObject parent;

    String name;

    Map<String, ObjectWrapper> params = new HashMap<String, ObjectWrapper>();

    public FinderType( Element element )
    {
        parse( element );
    }

    public StringBuilder toCode()
    {
        return toWatijCode();
    }

    public StringBuilder toWatijCode()
    {
        StringBuilder buf = new StringBuilder();
        buf.append( String.format( "%1$s(", this.name ) );
        for ( String key : params.keySet() )
        {
            buf.append( params.get( key ).toArgumentString() ).append( ", " );
        }
        if ( params.size() > 0 )
        {
            buf.delete( buf.length() - 2, buf.length() );
        }
        buf.append( ")" );
        return buf;
    }

    public void setParent( TestFireObject parent )
    {
        this.parent = parent;
    }

    public TestFireObject getParent()
    {
        return parent;
    }

    public void parse( Element el )
    {
        el = DomUtil.getChildElement( el );
        this.name = el.getLocalName();
        NamedNodeMap map = el.getAttributes();
        int len = map.getLength();
        for ( int i = 0; i < len; i++ )
        {
            Attr attr = (Attr)map.item( i );
            params.put( attr.getName(), new ObjectWrapper( attr ) );
        }

    }


    public Object translate( TestsetType testset )
    {
        return TranslatorFactory.translate(testset, this);
    }

    public String getName()
    {
        return name;
    }

    public ObjectWrapper getParameter(String key){
        return params.get( key );
    }

    public Map<String, ObjectWrapper> getParams()
    {
        return params;
    }
}

// eof: FinderType.java